library(gridExtra)
library(readstata13)
library(ggplot2)
library(gridExtra)
library(foreign)
library(MASS)
library(Matrix)
library(lattice)
library(rdd)
library(foreign)
library(ri)
library(scales)

rm(list = ls())

# Set working directory. Please set your own
setwd("~/Desktop/PSRM Replication")


theme_base1 <- 
  theme_minimal(base_size=14)  + 
  theme(legend.position=c(0.15, .9),  legend.key.size = unit(1,"line"),
        axis.text=element_text(size=14),axis.title.x=element_text(size=14),axis.title.y=element_text(size=14),
        plot.title = element_text(size=14, hjust= 0.5))



data <- read.dta("Data/sameaveragecontrols_no.dta")



data$grp<-as.factor(data$id)


main<-ggplot(data, aes(x=id, y =estimate)) +   
  geom_point(size = 2,position=position_dodge(0.35)) + 
  geom_errorbar(aes(ymax = max95, ymin = min95),width=0,  size=.7,position=position_dodge(width=0.35))+
  geom_errorbar(aes(ymax = max90, ymin = min90),width=0, size=1.6,position=position_dodge(width=0.35))+
  scale_y_continuous(limits = c(-.07, .15))+ 
  scale_x_continuous(name="Election Type",labels=c("Presidential","All","Midterm"),breaks = c(1,2,3),limits=c(0.75,3.25))+
  geom_hline(yintercept = 0, 
             linetype = 2, color = "black")+ ylab("ITT of Same-Election Eligibles") + xlab("")+
  theme_base1 + ggtitle("Difference in Probability of Voting:\n Same vs different type of election") 
main
ggsave("Graphs/Allaverage_no.pdf", width = 22, height = 18, units = "cm")  





data <- read.dta("Data/samealltogether_no.dta")



data$grp<-as.factor(data$id3)





main<-ggplot(data, aes(x=id, y =estimate ,color=grp)) +   
  geom_point(size = 2,position=position_dodge(0.35)) +
  geom_errorbar(aes(ymax = max95, ymin = min95),width=0,  size=.7,position=position_dodge(width=0.35))+
  geom_errorbar(aes(ymax = max90, ymin = min90),width=0, size=1.6,position=position_dodge(width=0.35))+
  scale_color_manual(name="Election",labels=c("Presidential","Same-Type", "Midterm"),values=c("blue","black","red")) + 
  scale_y_continuous(limits = c(-.07, .15))+ 
  scale_x_continuous(name="Election Counter",breaks = c(2,3,4,5,6,7,8,9,10,11,12,13,14,15),limits=c(1.75,15.25))+
  geom_hline(yintercept = 0, 
             linetype = 2, color = "black")+ ylab("ITT of Same-Election Eligibles") + xlab("")+
  theme_base1 + ggtitle("Difference in Probability of Voting:\n Same vs different type of election") 
main
ggsave("Graphs/Alltogether15_no.pdf", width = 22, height = 18, units = "cm")  




